package com.meihua.shellapp.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.bean.ProductIndexBean;
import com.meihua.shellapp.bean.ZhongAnBean;
import com.meihua.shellapp.utils.PicassoUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ${zhangyuanchao} on 2017/9/29.
 */

public class ContantRVAdapter extends RecyclerView.Adapter {
    private List<ProductIndexBean.DataBean.ProductBeanX.ProductBean> mData;
    private Activity activity;

    public ContantRVAdapter(List<ProductIndexBean.DataBean.ProductBeanX.ProductBean> product, Activity activity) {
        mData = product;
        this.activity = activity;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContantViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ContantViewHolder) holder).setData(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnItemClickListener mOnItemClickListener;

    class ContantViewHolder extends RecyclerView.ViewHolder {
        public TextView mConTv;
        public ImageView mConIv;

        public ContantViewHolder(View itemView) {
            super(itemView);
            mConIv = itemView.findViewById(R.id.item_con_iv);
            mConTv = itemView.findViewById(R.id.item_con_tv);
        }
        int[] imgs = {R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,R.drawable.t5,R.drawable.t6,R.drawable.t7};
        public void setData(final int position) {
            final ProductIndexBean.DataBean.ProductBeanX.ProductBean productBean = mData.get(position);
            mConTv.setText(productBean.getProduct_name());
            PicassoUtils.getInstance().bindImageData(mConIv, productBean.getProduct_img());
            //temp // TODO: 2017/12/12  
            //Picasso.with(this.itemView.getContext()).load(imgs[position]).placeholder(R.drawable.ic_default).into(mConIv);
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     /*打开产品详情界面*/
                    Intent intent = new Intent(activity, WebActivity.class);
//                    String url = "";
//                    switch (position){
//                        case 0:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000688&accountId=502325089";
//                            break;
//                        case 1:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000692&accountId=502325089";
//                            break;
//                        case 2:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000689&accountId=502325089";
//                            break;
//                        case 3:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000687&accountId=502325089";
//                            break;
//                        case 4:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000690&accountId=502325089";
//                            break;
//                        case 5:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000697&accountId=502325089";
//                            break;
//                        case 6:
//                            url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000695&accountId=502325089";
//                            break;
//                        default:
//                            break;
//                    }
                    intent.putExtra("url", mData.get(position).getUrl());
                    activity.startActivity(intent);
//                    Intent intent = new Intent(activity, WebActivity.class);
//                    intent.putExtra("url", URL.getUrl().WEBPRODUCT + productBean.getId() + "/" + MyApplication.getInstance().getMId());
//                    activity.startActivity(intent);
                }
            });
        }
    }
}
